<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor
        :itemList="[
          { name: 'box-base', title: '汇总信息' },
          { name: 'box-list', title: '汇总明细' },
        ]"
      />
    </div>

    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">汇总信息</div>
          <customFormItem
            label="申请科室"
            name="deptName"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="utilSelect.dept(selectDept)"
            :disabled="cpHasDtl"
          />
          <customFormItem label="业务单号" name="busKey" :parent="_self" :form="form" help='自动设置' disabled
              :config="{ defaultValue: '自动获取'}"
          />
          <customFormItem label="订单金额" name="amount" :parent="_self" :form="form"
                          :type="constCommon.formItemType.inputNumber.name" disabled
              :config="{ defaultValue: '自动获取'}"
          />
          <customFormItem label="业务日期" name="busDate" :parent="_self" :form="form" :type="constCommon.formItemType.datePicker.name" disabled />
          <customFormItem label="汇总申请人" name="busUser" :parent="_self" :form="form" disabled/>
          <customFormItem label="备注" name="remark" :parent="_self" :form="form"/>
        </template>
      </customForm>
      <div id="box-list" class="box-title">汇总明细</div>
      <data-view
        propViewKey="dept_demand_sum_item"
        :propInitData="form"
        ref="childDataView"
        :propNoEditRow="true"
        :propParentPage="_self"
        :propAutoFetchData="false"
        @extra-button-click="extraButtonClick"
        :propIsTdesign="true"
        :propTableHide="propParent.cpFormNew"
        propTableHideMsg="请先保存主单据再操作"
        :propsDisableDbClick="true"
        :propFilterBtn="propFilterBtn"
        :propParentData="propParent.viewFormData"
        :propFlowKey="propParent.propFlowKey"
        :propHideFlowSelect="true"
      />
    </div>

    <!--选择科室需求 / 需求单选择-->
    <tableDrawer
      ref="tableDrawerDemand"
      refName="tableDrawerDemand"
      viewKey="dept_demand_select"
      title="需求单选择"
      :widthPer="100"
      :heightPx="-1"
      :widthPx="-1"
      loadForce
    />

    <!--请购汇总-->
    <tableDrawer
      ref="tableDrawerDemandSum"
      refName="tableDrawerDemandSum"
      viewKey="dept_demand_sum_all"
      title="请购汇总"
      :queryWrapper="cpDemandSumWrapper"
      :widthPer="100"
      :heightPx="-1"
      :widthPx="-1"
      loadForce
    />
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'

export default {
  data() {
    return {
      form: {},
    }
  },
  created() {
    this.$emit('event-register', {
      validate: callback => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        let deptName = this.propDictionaries.deptId.filter(dept => this.form.deptId == dept.id).map(dept => dept.name)[0]
        this.$set(this.form, 'deptName', deptName)
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        let wrapper = this.getChildViewQueryWrapper()
        if (this.propStatus == 'addnew') {
          this.$set(this.form, 'busUser', this.utils.getCurrentUser().name)
          this.$set(this.form, 'busDate', new Date().format(this.dates.defaultDatePattern))
          this.$set(this.form, 'deptId', this.utils.getCurrentUser().deptId)
          this.$refs.childDataView.setQueryWrapper(wrapper, false)
        } else {
          this.$refs.childDataView.setQueryWrapper(wrapper)
        }
        if (!this.form.id) {
          this.$set(this.form, 'deptId', this.utils.getCurrentUser().deptId)
          this.$set(this.form, 'deptName', this.utils.getCurrentUser().deptName)
          this.$set(this.form, 'busUser', this.utils.getCurrentUser().name)
          this.$set(this.form, 'busDate', new Date().format(this.dates.defaultDatePattern))
        }
      },
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {},
      beforeFlow: flowButtonKey => {
        return true
      },
      afterFlow: flowButtonKey => {},
      beforeClose: () => {
        return true
      }
    })
  },
  computed: {
    // 是否有明细
    cpHasDtl() {
      return this.form.countDtl > 0
    },
    cpDemandSumWrapper() {
      let wrapper = this.$newWrapper()
      wrapper.eq('demandSumId', this.form.id)
      return wrapper
    },
  },
  methods: {
    // 选择需求科室
    selectDept(row) {
      this.$set(this.form, 'deptId', row.id)
      this.$set(this.form, 'deptName', row.name)
    },
       // 商品 新增、删除、修改后调用
    afterTableDataChange() {
      this.utils.refreshRefs(this)
    },
    loadDtl() {
      if (!this.$refs.childDataView) {
        return
      }
      let $newWrapper = this.$newWrapper()
      if (this.form.id) {
        $newWrapper.eq('demandSumId', this.form.id)
      } else {
        $newWrapper.ne(1, 1)
      }
      this.$refs.childDataView.setQueryWrapper($newWrapper)
    },

    getChildViewQueryWrapper() {
      if (this.form.id) {
        return this.$newWrapper().eq('demandSumId', this.form.id)
      } else {
        return this.$newWrapper().ne(1, 1)
      }
    },
    getChildRows() {
      if (!this.$refs.childDataView) {
        return []
      }
      return this.$refs.childDataView.getRows0()
    },

    // 添加需求单
    _saveBatchItem(list) {
      this.utilVue.loadShow(true)
      if (!list.length) {
        return
      }
      let params = {
        demandSumDTOS: list,
        demandSunId: this.form.id
      }
      this.$ajax.post('/dept/demand_sum/addByDemand', params).then(resp => {
        this.utilVue.loadHide()
        if (resp.code == 0) {
          this.utilMessage.success(resp.msg)
          this.loadDtl()
          this.afterTableDataChange()
        }
      })
    },

      // 扩展按钮事件
    extraButtonClick({ button, data, index }) {
      let buttonKey = button.key
      if (buttonKey === 'selectDemand') { //选择需求单
        this.$refs.tableDrawerDemand.show(this._saveBatchItem, true)
      }

      if (buttonKey === 'demandSum') { //请购汇总
        this.$refs.tableDrawerDemandSum.show()
      }
    },

    // 过滤显示按钮
    propFilterBtn(btnList) {
      // 只读状态，隐藏相关操作按钮
      if (this.propReadonly) {
        btnList =  btnList.filter(btn => !['selectDemand', 'demandSum'].includes(btn.code))
      }
      return btnList
    },
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  components: {customAnchor},
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
}
</style>
